#include "Textures.h"

BMPClass Textures::textureImages[4];
GLuint Textures::textureIds[4];

vector<GLuint> Textures::previewImageIds;
vector<BMPClass*> Textures::previewImages;

void Textures::pushPreview(string filePath)
{
	if(filePath=="")
	{
		previewImages.push_back(new BMPClass());
		previewImageIds.push_back(GLuint());
		return;
	}
	cout << " Size " <<  previewImages.size() << endl;/////////////////
	previewImages.push_back(new BMPClass());
	previewImageIds.push_back(GLuint());
	BMPLoad(filePath,*previewImages[previewImages.size()-1]);
	glGenTextures(1,&previewImageIds[previewImageIds.size()-1]);

	glBindTexture(GL_TEXTURE_2D, previewImageIds[previewImageIds.size()-1]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	// Linear Filtering
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Linear Filtering
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, previewImages[previewImages.size()-1]->width, previewImages[previewImages.size()-1]->height, GL_RGB, GL_UNSIGNED_BYTE, previewImages[previewImages.size()-1]->bytes);

}

void Textures::initialize()
{
	BMPLoad("data/textures/WATER.BMP",textureImages[0]); // Water Texture
	BMPLoad("data/textures/TERRAIN.BMP",textureImages[1]); // Terrain Texture
	BMPLoad("data/textures/SKYBOX.BMP",textureImages[2]); // Sky Texture
	BMPLoad("data/textures/NIGHT.BMP",textureImages[3]); // Night Texture

	glGenTextures(1, &textureIds[0]); // Create Water Texture
	glGenTextures(1, &textureIds[1]); // Create Terrain Texture
	glGenTextures(1, &textureIds[2]); // Create Sky Texture
	glGenTextures(1, &textureIds[3]); // Create Night Texture

	// Generate Water Texture
	glBindTexture(GL_TEXTURE_2D, textureIds[0]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	// Linear Filtering
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Linear Filtering
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, textureImages[0].width, textureImages[0].height, GL_RGB, GL_UNSIGNED_BYTE, textureImages[0].bytes);

	// Generate Terrain Texture
	glBindTexture(GL_TEXTURE_2D, textureIds[1]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	// Linear Filtering
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Linear Filtering
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, textureImages[1].width, textureImages[1].height, GL_RGB, GL_UNSIGNED_BYTE, textureImages[1].bytes);

	// Generate Sky Texture
	glBindTexture(GL_TEXTURE_2D, textureIds[2]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	// Linear Filtering
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Linear Filtering
	glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, textureImages[2].width, textureImages[2].height, GL_RGB, GL_UNSIGNED_BYTE, textureImages[2].bytes);

	// Generate Night Texture
	glBindTexture(GL_TEXTURE_2D, textureIds[3]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	// Linear Filtering
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Linear Filtering
	glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, textureImages[3].width, textureImages[3].height, GL_RGB, GL_UNSIGNED_BYTE, textureImages[3].bytes);
	//glTexImage2D(GL_TEXTURE_2D, 0, 3, textureImages[2].width, textureImages[2].height, 0, GL_RGB, GL_UNSIGNED_BYTE, textureImages[2].bytes);
}


